<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <ButtonGroup>
          <a-button type="primary" @click="handleCreate"> + 新增字典 </a-button>
        </ButtonGroup>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                // icon: 'clarity:note-edit-line',
                label: '编辑',
                ifShow: record.systemFlag === '0',
                onClick: handleEdit.bind(null, record),
              },
              {
                // icon: 'ant-design:appstore-filled',
                label: '字典项',
                onClick: handleAddItem.bind(null, record),
              },
              {
                // icon: 'ant-design:delete-outlined',
                label: '删除',
                // color: 'error',
                ifShow: record.systemFlag === '0',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <DictModal @register="registerModal" @success="handleSuccess" />

    <!-- 字典项编辑弹框 -->
    <DictEditItemModal
      @register="registerEditItemModal"
      @success="handleItemSuccess"
      @cancel="handleItemCancel"
    />

    <!-- 字典项弹框 -->
    <DictItemModal
      :footer="null"
      @register="registerItemModal"
      @edit-dict-item="handleEditDictItem"
    />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { Button } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import DictModal from './DictModal.vue';
  import DictItemModal from './DictItemModal.vue';
  import DictEditItemModal from './DictEditItemModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { setTableEmptyHeight } from '/@/utils';

  import { getDictPageListApi, deleteDictApi } from '/@/api/admin/dict';
  import { columns, searchFormSchema } from './dict.data';
  const ButtonGroup = Button.Group;

  export default defineComponent({
    name: 'PostManagement',
    components: {
      ButtonGroup,
      BasicTable,
      DictModal,
      TableAction,
      DictItemModal,
      DictEditItemModal,
    },
    setup() {
      const [registerModal, { openModal }] = useModal();
      const [registerItemModal, { openModal: openItemModal }] = useModal();

      const [registerEditItemModal, { openModal: openEditItemModal }] = useModal();

      const { createMessage } = useMessage();

      const [registerTable, { reload }] = useTable({
        api: getDictPageListApi,
        columns,
        // descs
        beforeFetch(param) {
          return {
            ...param,
            descs: 'create_time',
          };
        },
        afterFetch(res) {
          setTableEmptyHeight(res);
        },
        formConfig: {
          labelWidth: 75,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        // showIndexColumn: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: 'right',
        },
        indexColumnProps: {
          width: 150,
          align: 'left',
        },
      });

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleAddItem(record: Recordable) {
        openItemModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        console.log(record);
        await deleteDictApi(record.id);
        createMessage.success('操作成功');
        reload();
      }

      function handleSuccess() {
        reload();
      }

      //字典项新增，编辑
      function handleEditDictItem(record) {
        let isUpdate = false;
        if (record.id != undefined) {
          isUpdate = true;
        }
        openEditItemModal(true, {
          record,
          isUpdate,
        });
      }

      function handleItemSuccess() {
        openItemModal(true, {
          isUpdate: true,
        });
      }
      function handleItemCancel() {
        openItemModal(true, {
          isUpdate: false,
        });
      }

      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleAddItem,
        registerItemModal,
        handleEditDictItem,
        handleItemSuccess,
        handleItemCancel,
        registerEditItemModal,
        openEditItemModal,
      };
    },
  });
</script>

<style scoped lang="less">
  :deep(.vben-basic-table .ant-table-wrapper) {
    padding-left: 16px;
    padding-top: 16px;
  }

  :deep(.vben-basic-table .ant-table-wrapper .ant-table-title) {
    padding-bottom: 16px !important;
  }
</style>
